package com.zuo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zuo.entity.Bulletin;
import com.zuo.entity.User;
import com.zuo.service.BulletinService;
import com.zuo.util.HrmConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author 白白旧维
 * @Date 2022/3/3 11:08
 */
@Controller
public class BulletinController {

    @Autowired
    private BulletinService bulletinService;

    @RequestMapping("/notice")
    public String notice(Model model, @RequestParam(value = "page",defaultValue = "1") int page){
        PageHelper.startPage(page, HrmConstants.PAGE_DEFAULT_SIZE);

        List<Bulletin> bulletins = bulletinService.listBulletin();

        //PageInfo一定要获取到数据库集合之后
        PageInfo<Bulletin> pageInfo = new PageInfo<>(bulletins);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("notices",bulletins);
        model.addAttribute("type","all");
        return "notice/notice.jsp";
    }

    @RequestMapping("/searchNotice")
    public String searchNotice(String bname,String bcontent,Model model, @RequestParam(value = "page",defaultValue = "1") int page){
        PageHelper.startPage(page,HrmConstants.PAGE_DEFAULT_SIZE);

        List<Bulletin> bulletins = bulletinService.searchBulletin(bname, bcontent);

        //PageInfo一定要获取到数据库集合之后
        PageInfo<Bulletin> pageInfo = new PageInfo<>(bulletins);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("notices",bulletins);
        model.addAttribute("type","search");
        model.addAttribute("bname",bname);
        model.addAttribute("bcontent",bcontent);
        return "notice/notice.jsp";
    }

    @RequestMapping("/previewNotice")
    public String previewNotice(int id,Model model){
        Bulletin bulletin = bulletinService.getOne(id);
        model.addAttribute("notice",bulletin);
        return "notice/previewNotice.jsp";
    }

    @RequestMapping("/toUpdateNotice")
    public String toUpdateNotice(int id,Model model){
        Bulletin bulletin = bulletinService.getOne(id);
        model.addAttribute("notice",bulletin);
        return "notice/showUpdateNotice.jsp";
    }

    @RequestMapping("/updateNotice")
    public String updateNotice(Bulletin bulletin,Model model){
        int result = bulletinService.updateBulletin(bulletin);
        if(result!=0){
            model.addAttribute("info","成功修改了"+result+"条数据");
        }else{
            model.addAttribute("info","修改失败");
        }
        return "forward:notice.action";
    }

    @RequestMapping("/removeNotice")
    public String removeNotice(String ids,Model model){
        int result = bulletinService.removeBulletins(ids);
        model.addAttribute("info","成功删除了"+result+"条记录");
        return "forward:notice.action";
    }

    @RequestMapping("/toAddNotice")
    public String toAddNotice(){
        return "notice/showAddNotice.jsp";
    }

    @RequestMapping("/addNotice")
    public String addNotice(Bulletin bulletin,Model model){
        int result = bulletinService.addBulletin(bulletin);
        if(result!=0){
            model.addAttribute("info","添加成功");
        }else{
            model.addAttribute("info","添加失败");
        }
        return "forward:notice.action";
    }

}
